home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / LOGGER.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  9KB  |  258 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    l o g g e r . c                                                 */
  3. /*                                                                    */
  4. /*    Logging functions for UUPC/extended                             */
  5. /*                                                                    */
  6. /*    Copyright (c) 1992 by Kendra Electronic Wonderworks; all        */
  7. /*    rights reserved except those explicitly granted by the          */
  8. /*    UUPC/extended license.                                          */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*                          RCS Information                           */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*
  16.  *    $Id: LOGGER.C 1.7 1993/04/11 00:32:05 ahd Exp $
  17.  *
  18.  *    Revision history:
  19.  *    $Log: LOGGER.C $
  20.  *     Revision 1.7  1993/04/11  00:32:05  ahd
  21.  *     Global edits for year, TEXT, etc.
  22.  *
  23.  *     Revision 1.6  1993/03/06  22:48:23  ahd
  24.  *     Drop dashes between log entries
  25.  *
  26.  *     Revision 1.5  1993/01/23  19:08:09  ahd
  27.  *     Correct sleep.h include
  28.  *
  29.  * Revision 1.4  1992/11/23  03:56:06  ahd
  30.  * Do not use expand_path to build log file name
  31.  * Use strpool for names
  32.  *
  33.  * Revision 1.3  1992/11/22  20:58:55  ahd
  34.  * Move retry of opens to FOPEN()
  35.  *
  36.  * Revision 1.2  1992/11/19  02:58:22  ahd
  37.  * drop rcsid
  38.  *
  39.  * Revision 1.1  1992/11/16  05:00:26  ahd
  40.  * Initial revision
  41.  *
  42.  */
  43.  
  44. /*--------------------------------------------------------------------*/
  45. /*                   Standard library include files                   */
  46. /*--------------------------------------------------------------------*/
  47.  
  48. #include <stdio.h>
  49. #include <time.h>
  50. #include <sys/types.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53.  
  54. /*--------------------------------------------------------------------*/
  55. /*                    UUPC/extended include files                     */
  56. /*--------------------------------------------------------------------*/
  57.  
  58. #include "lib.h"
  59. #include "dater.h"
  60. #include "expath.h"
  61. #include "logger.h"
  62. #include "hlib.h"
  63. #include "timestmp.h"
  64. #include "ssleep.h"
  65.  
  66. /*--------------------------------------------------------------------*/
  67. /*                      Define current file name                      */
  68. /*--------------------------------------------------------------------*/
  69.  
  70. currentfile();
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*                          Local variables                           */
  74. /*--------------------------------------------------------------------*/
  75.  
  76. static char *logname  = NULL;
  77. static char *tempname = NULL;
  78.  
  79. static void copylog( void );
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*    o p e n l o g                                                   */
  83. /*                                                                    */
  84. /*    Begin logging to a standard file name                           */
  85. /*--------------------------------------------------------------------*/
  86.  
  87. void openlog( const char *log )
  88. {
  89.    char fname[FILENAME_MAX];
  90.  
  91. /*--------------------------------------------------------------------*/
  92. /*                Create the final log name for later                 */
  93. /*--------------------------------------------------------------------*/
  94.  
  95.    logname =  (char*) ((log == NULL) ? compilen : log);
  96.    tempname = strchr( logname, '.');
  97.    mkfilename( fname, E_spooldir, logname );
  98.  
  99.    if ( tempname == NULL )
  100.       strcat( fname, ".LOG" );
  101.    logname = newstr( fname );
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*                   Create temporary log file name                   */
  105. /*--------------------------------------------------------------------*/
  106.  
  107.    if ( bflag[F_MULTITASK] )
  108.    {
  109.       char *savedir = E_tempdir;    /* Save real tempory directory   */
  110.  
  111.       E_tempdir = E_spooldir;       /* Create log file in spool dir
  112.                                        to allow for larger files
  113.                                        and/or system crashes         */
  114.       tempname = newstr( mktempname(fname, "LOG"));
  115.                                     /* Get the file name             */
  116.       E_tempdir = savedir;          /* Restore true temp dir         */
  117.    } /* if */
  118.    else
  119.       tempname = logname;           /* Log directly to true log file */
  120.  
  121.    full_log_file_name = tempname;   /* Tell printmsg() what our log
  122.                                        file name is                  */
  123.  
  124. /*--------------------------------------------------------------------*/
  125. /*                    Open the temporary log file                     */
  126. /*--------------------------------------------------------------------*/
  127.  
  128.    logfile = FOPEN( tempname , "a",TEXT_MODE );
  129.                               /* We append in case we are not in
  130.                                  multitask mode and we do not want
  131.                                  to clobber the real log!            */
  132.  
  133.    if ( logfile == NULL )
  134.    {
  135.       printerr( tempname );
  136.       panic();
  137.    }
  138.  
  139. /*--------------------------------------------------------------------*/
  140. /*               Request the copy function be run later               */
  141. /*--------------------------------------------------------------------*/
  142.  
  143.    atexit( copylog );
  144.  
  145. /*--------------------------------------------------------------------*/
  146. /*    Tag the new log file with the current time and program date.    */
  147. /*    We don't use printmsg() because that will not display the       */
  148. /*    time if debugging is turned up.                                 */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.    fprintf(logfile,
  152.                "%s %s: %s %s (%s %s)\n",
  153.                dater( time( NULL ), NULL),
  154.                compilen, compilep, compilev, compiled, compilet);
  155.  
  156.    if ( ferror( logfile ))
  157.    {
  158.       printerr( tempname );
  159.       panic();
  160.    }
  161.  
  162. } /* openlog */
  163.  
  164. /*--------------------------------------------------------------------*/
  165. /*    c o p y l o g                                                   */
  166. /*                                                                    */
  167. /*    Close and copy a log opened by openlog                          */
  168. /*--------------------------------------------------------------------*/
  169.  
  170. static void copylog( void )
  171. {
  172.  
  173.    FILE *input;
  174.    FILE *output;
  175.    char buf[BUFSIZ];
  176.    int chars_read, chars_written;
  177.  
  178. /*--------------------------------------------------------------------*/
  179. /*   If not multitasking, just close the file and exit gracefully     */
  180. /*--------------------------------------------------------------------*/
  181.  
  182.    if ( !bflag[ F_MULTITASK ] )
  183.    {
  184.       fclose( logfile );
  185.       logfile = stdout;
  186.       return;
  187.    }
  188.  
  189. /*--------------------------------------------------------------------*/
  190. /*            We're multitasking; copy the file gracefully            */
  191. /*--------------------------------------------------------------------*/
  192.  
  193.    output = FOPEN( logname ,"a",TEXT_MODE);
  194.  
  195.    if ( output == NULL )
  196.    {
  197.       printmsg(0,"Cannot merge log %s to %s", tempname, logname );
  198.       printerr( logname );
  199.       fclose( logfile );
  200.       logfile = stderr;
  201.       return;
  202.    }
  203.  
  204.    fclose( logfile );
  205.    logfile = output;                /* Log directly into real file   */
  206.    full_log_file_name = logname;    /* Tell printerr we switched     */
  207.  
  208.    input = FOPEN( tempname, "r",TEXT_MODE );
  209.  
  210.    if ( input == NULL )
  211.    {
  212.       printerr( tempname );
  213.       fclose( input );
  214.       fclose( output );
  215.       logfile = stdout;
  216.    }
  217.  
  218. /*--------------------------------------------------------------------*/
  219. /*           File is open, copy temporary log to end of it            */
  220. /*--------------------------------------------------------------------*/
  221.  
  222.    while ((chars_read = fread(buf,sizeof(char), BUFSIZ, input)) != 0)
  223.    {
  224.       chars_written = fwrite(buf, sizeof(char), chars_read, output );
  225.  
  226.       if (chars_written != chars_read)
  227.       {
  228.          printerr( logname );
  229.          clearerr( output );
  230.          fclose( input );
  231.          fclose( output );
  232.          logfile  = stdout;
  233.          return;
  234.       }
  235.    } /* while */
  236.  
  237. /*--------------------------------------------------------------------*/
  238. /*                     Check for errors on input                      */
  239. /*--------------------------------------------------------------------*/
  240.  
  241.    if ( ferror( input ))
  242.    {
  243.       printerr( tempname );
  244.       clearerr( input );
  245.    }
  246.  
  247. /*--------------------------------------------------------------------*/
  248. /*             Close up shop and discard temporary input              */
  249. /*--------------------------------------------------------------------*/
  250.  
  251.    fclose( input );
  252.    fclose( output );
  253.    logfile  = stdout;
  254.  
  255.    unlink( tempname );
  256.  
  257. } /* copylog */
  258.